<?php

class Lib_Router {

    static $modules;
    static $defaultModule;
    static $defaultController;
    static $defaultAction;
    static $defaultParams;

    static function init() {
        /**
         * Loading modules 
         */
        Lib_Router::$modules = Lib_Router::getModules();

        /**
         * Loading default config
         * 
         */
        Lib_Router::$defaultModule = Lib_Config::$config['defaultModule'];
        Lib_Router::$defaultController = Lib_Config::$config['defaultController'];
        Lib_Router::$defaultAction = Lib_Config::$config['defaultAction'];
        Lib_Router::$defaultParams = Lib_Config::$config['defaultParams'];
    }

    static function getBase() {
        return $_SERVER['PHP_SELF'];
    }

    static function getModules() {
        $files = scandir("app");
        $result = array();
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                if (is_dir("app/$file")) {
                    $result[] = $file;
                }
            }
        }
        return $result;
    }

    static function setDefaultController($controller) {
        Lib_Router::$defaultController = $controller;
    }

    static function setDefaultAction($action) {
        Lib_Router::$defaultAction = $action;
    }

    static function setDefaultModule($module) {
        Lib_Router::$defaultModule = $module;
    }

    static function setDefaultParams($params) {
        Lib_Router::$defaultParams = $params;
    }

    static function route($string) {
        Lib_Router::init();
        /**
         * Separate URL into components 
         */
        $root = dirname($_SERVER['PHP_SELF']);
        $temp = $string;
        $contains = strpos($string, $root);
        if (!is_bool($contains)){
            $temp = substr($string, strlen($root) + 1, strlen($string) - 1);
        }
        $part = explode('/', $temp);
        $cacheKey = join('_', $part);
        $cacheKey = Lib_Config::$config['cachePrefix'] . '_' . $cacheKey;

        //if (!ZelCache::exist($cacheKey)) {
            /**
             * Routing and execute controller 
             */
            if (count($part) >= 2) {
                //has module name in url
                if (in_array($part[0], Lib_Router::$modules)) {
                    $module = Lib_Util::upperCaseWord($part[0]);
                    $controllerClass = Lib_Util::upperCaseWord($part[1]);
                    $controllerClass = 'App_'.$module.'_Controller_'.$controllerClass;
                    $controller = new $controllerClass;
                    $action = $part[2];
                    if (strcmp(strtolower($_SERVER['REQUEST_METHOD']), 'get') == 0) {
                        $params = array_slice($part, 3, count($part)-1);
                    } else {
                        $params = $_POST;
                    }
                } else { //otherwise
                    $module = Lib_Util::upperCaseWord(Lib_Router::$defaultModule);
                    $controllerClass = Lib_Util::upperCaseWord($part[0]);
                    $controllerClass = 'App_'.$module.'_Controller_'.$controllerClass;
                    $controller = new $controllerClass();
                    $action = $part[1];
                    if (strcmp(strtolower($_SERVER['REQUEST_METHOD']), 'get') == 0) {
                        $params = array_slice($part, 2, count($part)-1);
                    } else {
                        $params = $_POST;
                    }
                }
                $output = $controller->execute($module, $action, $params);
                echo $output;
                //ZelCache::put($cacheKey, $output);
            } else {
                $module = Lib_Util::upperCaseWord(Lib_Router::$defaultModule);
                $action = Lib_Router::$defaultAction;
                $params = Lib_Router::$defaultParams;
                $controllerClass = Lib_Router::$defaultController;
                $controllerClass = 'App_'.$module.'_Controller_'.$controllerClass;
                $controller = new $controllerClass();
                $output = $controller->execute($module, $action, $params);
                echo $output;
                //ZelCache::put($cacheKey, $output);
            }
//        } else {
//            $output = ZelCache::get($cacheKey);
//            echo $output;
//        }
    }

}

?>
